<style lang="scss">

</style>
<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-dialogDrag
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      >
      <el-form v-loading="loading" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="studentNo">
          <el-input v-model="form.studentNo" placeholder="请输入学生学号" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard" maxlength="18">
          <el-input v-model="form.idcard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="学校班级" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请选择学校班级" readonly/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

  import { getSchoolStudent, addSchoolStudent, updateSchoolStudent } from "@/api/school/schoolStudent";

  export default {
      name: "SchoolStudentEdit",
      data(){
        return {
          open: true,
          loading: false,
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            studentNo: [
              { required: true, message: "学号不能为空", trigger: "blur" }
            ],
            name: [
              { required: true, message: "学生姓名不能为空", trigger: "blur" }
            ],
            idcard: [
              { required: true, message: "身份证号不能为空", trigger: "blur" }
            ],
          },
        }
      },
      props:{
        id: {
          type: Number, default: null,
        },
        idcard: {
          type: String, default: null,
        },
        title: {
          type: String, default: '学生信息',
        }
      },
      created(){
        if(!this.$yhFunc.isEmpty(this.id)){
          this.loading = true;
          this.reset();
          getSchoolStudent(this.id).then(response => {
            this.form = response.data;
            this.loading = false;
          });
        }

      },
      methods: {
        /** 提交按钮 */
        handleSubmit(){
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.id != null) {
                updateSchoolStudent(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.$emit('handleSubmit');
                });
              } else {
                addSchoolStudent(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.$emit('handleSubmit');
                });
              }
            }
          });
        },
        handleClose(){
          this.$emit('handleClose');
        },
        // 表单重置
        reset() {
          this.form = {
            id: null,
            uuid: null,
            idcard: null,
            name: null,
            sex: null,
            deptId: null,
            status: "0",
            delFlag: null,
            createBy: null,
            createTime: null,
            updateBy: null,
            updateTime: null,
            remark: null
          };
          this.resetForm("form");
        },
      }

  }
</script>
